const { DataTypes, Model, Deferrable } = require("sequelize");
const sequelize = require("../../sequelize");
/**
 * 权限模型
 */
class Rule extends Model {
    /**
     * 异步递归父查子权限
     */
    static async getRules(object) {
        let rule=await Rule.findAll({
            where: {
                rule_id:object.id
            }
        })
        if (rule.length==0) {
            delete object["child"]
            return object
        }
        for (let i = 0; i < rule.length; i++) {
            let js=rule[i].toJSON()
            js["child"]=[]
            object["child"].push(Rule.getRules(js))
        }
        object["child"]=await Promise.all(object["child"])
        return object
    }
};
Rule.init({
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    rule_id: {
        type: DataTypes.INTEGER
    },
    status: {
        type: DataTypes.TINYINT(1),
        defaultValue: 0
    },
    name: {
        type: DataTypes.STRING
    },
    desc: {
        type: DataTypes.STRING
    },
    frontpath: {
        type: DataTypes.STRING
    },
    condition: {
        type: DataTypes.STRING
    },
    menu: {
        type: DataTypes.TINYINT,
        defaultValue: 0
    },
    order: {
        type: DataTypes.INTEGER
    },
    icon: {
        type: DataTypes.STRING
    },
    method: {
        type: DataTypes.STRING
    }
}, {
    sequelize,
    modelName: "Rule",
    tableName: "rule",
    createdAt: "create_time",
    updatedAt: "update_time",
})
module.exports = Rule